package 作业;

import jdk.jfr.Label;

import java.util.*;

public class BookHanger {
    public static void main(String[] args) {
        System.out.println("欢迎来到龙院图书管理系统，请登陆");
        boolean flog=false;
        boolean flog1=true;
      while(flog1){
          int q = 0;
          int r=0;
          String [][] book = new String[10][5];
          String[][] publicBook=new String[10][3];
          Scanner sc = new Scanner(System.in);
          Map<String, String> map = new HashMap<>();
          map.put("林海权", "12345");
          map.put("黄伟业", "123456");
          for (int i = 0; i < 10; i++) {
              for (int y = 0; y < 4; y++) {
                  book[i][y] = "0";
              }
          }
          for (int i = 0; i < 10; i++) {
              for (int y = 0; y < 3; y++) {
                  publicBook[i][y] = "0";
              }
          }
         if( check(map).equals("0")){
             flog=true;
         }

          while (flog) {
              System.out.println("-----欢迎来到图书管理系统-----");
              System.out.println("1 图书管理" + "\t" + "2 出版社管理" + "\t" + "3 退出登入" + "\t" + "4 退出系统");
              int h = sc.nextInt();
              if(h==1){
                  while(true){
                      System.out.println("输入数字进行选择");
                      System.out.println("1.增加" + "\t" + "2.删除" + "\t" + "3.更新" + "\t" + "4.查询" + "\t" + "5.返回上一级");
                      int s = sc.nextInt();
                      if(s==1){
                          add(book, q);
                          q+=1;
                      }else if(s==2){
                          remove(book,q);
                      }else if(s==3) {
                          update(book);
                      }else if(s==4){
                          select(book);
                      }else if(s==5){
                          break;
                      }
                  }
              }else if(h==2){
                  while(true){
                      System.out.println("请输入数字进行选择");
                      System.out.println("1.添加"+ "\t" + "2.删除" + "\t" + "3.更新" + "\t" + "4.根据出版社名称查询" + "\t" + "5.查询所有出版社"+"\t"+"6.返回上一级");
                      int f=sc.nextInt();
                      if(f==1){
                          add1(publicBook,r);
                          r+=1;
                      }else if(f==2){
                          remove1(publicBook);
                      }else if(f==3) {
                          update1(publicBook);
                      }else if(f==4){
                          select1(publicBook);
                      }else if(f==6){
                          break;
                      }else if(f==5){
                          for(int u=0;u<10;u++){
                              System.out.println(publicBook[u][0]);
                              System.out.println(publicBook[u][1]);
                              System.out.println(publicBook[u][2]);
                          }

                      }
                  }
              }
              else if(h==3){
                  flog=false;
              }else if(h==4){
                  flog=false;
                  flog1=false;
              }
          }
      }
        }
    public static String check(Map map) {
        String u="0";
        while (true) {
            Scanner s = new Scanner(System.in);
            System.out.println("请输入账号");
            String zhanghao = s.nextLine();
            System.out.println("请输入密码");
            String mima = s.nextLine();
           try{
               if (map.containsKey(zhanghao) & map.get(zhanghao).equals(mima)) {
               }
           }catch (NullPointerException e){
               System.out.println("输入有误请重新输入");
               continue;}
               System.out.println("登入成功");
               break;
           }
        return u;
        }
    public static void add(String[][] book, int i) {
        if (i > 10) {
            System.out.println("你不能再添加书");
        } else {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入图书ISBN：");
            String ISBN = sc.nextLine();
            book[i][0] = ISBN;
            System.out.println("请输入书名");
            String name = sc.nextLine();
            book[i][1] = name;
            System.out.println("请输入价格");
            String price = sc.nextLine();
            book[i][2] = price;
            System.out.println("请输入出版社");
            String s = sc.nextLine();
            book[i][3] = s;
            System.out.println("请输入作者");
            String r = sc.nextLine();
            book[i][4] = r;
            System.out.println("添加成功");
        }

    }

    public static void remove(String[][] book,int e) {
        int o = 0;
        System.out.println("请输入要删除的书名");
        Scanner SC = new Scanner(System.in);
        String s = SC.nextLine();
        for (int i = 0; i < 10; i++) {
            for (int u = 0; u < 4; u++) {
                if (book[i][u].equals(s)) {
                    for (int index = i; index < 10; index++) {
                        for (int h = 0; h < 4; h++) {
                            book[index][h] = book[index + 1][h];
                        }
                    }
                    e--;
                    System.out.println("删除成功");
                    break;
                }
                if (i == 9 & u == 3) {
                    o = 1;
                }
            }
        }
        if (o == 1) {
            System.out.println("你输入的书有误,请重新调用并输入");
        }


    }

    public static void update(String[][] book) {
        boolean flag = true;
        while (flag) {
            System.out.println("请输入ISBN");
            Scanner sc = new Scanner(System.in);
            String D = sc.nextLine();
            for (int u = 0; u < 10; u++) {
                if (book[u][0].equals(D)) {
                    System.out.println("请输入新的书名");
                    String t = sc.nextLine();
                    book[u][1] = t;
                    System.out.println("请输入新的价格");
                    String r = sc.nextLine();
                    book[u][2] = r;
                    System.out.println("请输入新的出版社");
                    String s = sc.nextLine();
                    book[u][3] = s;
                    System.out.println("请输入新的作者");
                    String q = sc.nextLine();
                    book[u][4] = q;

                    flag = false;
                    break;
                } else {
                    System.out.println("你输入的ISBN有误，请重新输入");
                    break;
                }
            }
        }
    }

    public static void select(String[][] book) {
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("1 ISBN"+"\t"+"2 书名(模糊查询)"+"\t"+"3 出版社"+"\t"+"4 作者"+"\t"+"5 价格范围"+"\t"+"6 查询所有"+"7 退出");
            System.out.println("请输入想要查询的本书信息");
            int t=sc.nextInt();
            if(t==1){
                String s = sc.next();
                for (int u = 0; u < 10; u++) {
                    if (book[u][1].equals(s)) {
                        System.out.printf("%-12s\t%-7s\t%-10s\t%-10s\t%-10s\n",book[u][0],book[u][1],book[u][2],book[u][3],book[u][4]);
                    }
                }
            }else if(t==2){
                String s = sc.nextLine();
                for (int u = 0; u < 10; u++) {
                    if (book[u][1].equals(s)) {
                        System.out.printf("%-12s\t%-7s\t%-10s\t%-10s\t%-10s\n",book[u][0],book[u][1],book[u][2],book[u][3],book[u][4]);
                    }
            }}else if(t==3){
                    String s = sc.nextLine();
                    for (int u = 0; u < 10; u++) {
                        if (book[u][3].equals(s)) {
                            System.out.printf("%-12s\t%-7s\t%-10s\t%-10s\t%-10s\n",book[u][0],book[u][1],book[u][2],book[u][3],book[u][4]);
                        }
            }}else if(t==4){
                    String s = sc.nextLine();
                    for (int u = 0; u < 10; u++) {
                        if (book[u][4].equals(s)) {
                            System.out.printf("%-12s\t%-7s\t%-10s\t%-10s\t%-10s\n",book[u][0],book[u][1],book[u][2],book[u][3],book[u][4]);
                        }
                }

            }else if(t==5){
                System.out.println("请输入最低价格");
                int min= sc.nextInt();
                System.out.println("请输入最高价格");
                int max = sc.nextInt();
                for(int h=0;h<10;h++){
                    Double D=Double.parseDouble(book[h][2]);
                    if(D>min & D<max){
                        System.out.printf("%-12s\t%-7s\t%-10s\t%-10s\t%-10s\n",book[h][0],book[h][1],book[h][2],book[h][3],book[h][4]);
                    }
                }
            }else if(t==6){
                for(int h=0;h<10;h++){
                    System.out.printf("%-12s\t%-7s\t%-10s\t%-10s\t%-10s\n",book[h][0],book[h][1],book[h][2],book[h][3],book[h][4]);
                }
            }else if(t==7){
                break;
            }


            }

        }
    public static void add1(String[][] pBook, int i){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入出版社名称");
        String F= sc.nextLine();
        pBook[i][0]=F;
        System.out.println("请输入出版社电话");
        String E=sc.nextLine();
        pBook[i][1]=E;
        System.out.println("请输入出版社地址");
        String f=sc.nextLine();
        pBook[i][2]=f;
        System.out.println("添加成功");
    }
    public static void remove1(String[][] pBook){
        int o = 0;
        System.out.println("请输入要删除的出版社");
        Scanner SC = new Scanner(System.in);
        String s = SC.nextLine();
        for (int i = 0; i < 10; i++) {
            for (int u = 0; u < 3; u++) {
                if (pBook[i][u].equals(s)) {
                    for (int index = i; index < 10; index++) {
                        for (int h = 0; h < 4; h++) {
                            pBook[index][h] = pBook[index + 1][h];
                        }
                    }
                    System.out.println("删除成功");
                    break;
                }
                if (i == 9 & u == 2) {
                    o = 1;
                }
            }
        }
        if (o == 1) {
            System.out.println("你输入的书有误,请重新调用并输入");
        }
    }
    public static void update1(String[][] pBook){
        boolean flag = true;
        while (flag) {
            System.out.println("请输入出版社");
            Scanner sc = new Scanner(System.in);
            String D = sc.nextLine();
            for (int u = 0; u < 10; u++) {
                if (pBook[u][0].equals(D)) {
                    System.out.println("请输入新的出版社名称");
                    String t = sc.nextLine();
                    pBook[u][0] = t;
                    System.out.println("请输入新的出版社电话");
                    String r = sc.nextLine();
                   pBook[u][1] = r;
                    System.out.println("请输入新的出版社");
                    String s = sc.nextLine();
                    pBook[u][2] = s;
                    flag = false;
                    break;
                } else {
                    System.out.println("你输入的出版社有误，请重新输入");
                    break;
                }
            }
        }
    }
    public static void select1(String[][] pBook){
        while (true) {
            System.out.println("请输入要查询的出版社名称退出请按q");
            Scanner sc = new Scanner(System.in);
            String s = sc.nextLine();
            if(s.equals("q")){
                break;
            }
            for (int u = 0; u < 10; u++) {
                if (pBook[u][0].equals(s)) {
                    System.out.println("请输入想要查询的出版社信息");
                    System.out.println("1 出版社地址"+"\t"+"2 电话"+"\t"+"3 退出");
                    int t=sc.nextInt();
                    if(t==1){
                        System.out.println("出版社地址："+pBook[u][1]);
                    }else if(t==2){
                        System.out.println("出版社电话"+pBook[u][2]);
                    }else if(t==3){
                        break;
                    }

                }

            }
        }
    }
}

